package com.atguigu.gulimall.search.app;

import com.atguigu.common.exception.BizCodeException;
import com.atguigu.common.to.es.SkuEsModel;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.search.service.ElasticProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * 保存ES
 *
 * @author DuGuanghan
 * @since 2024-04-09 14:21:11
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSaveController {

    @Autowired
    ElasticProductService elasticProductService;

    @PostMapping("/up")
    public R upSpu(@RequestBody List<SkuEsModel> skuEsModelList) {
        Boolean result = false;
        try {
            result = elasticProductService.saveSpu(skuEsModelList);
        } catch (IOException e) {
            log.error("ElasticSaveController商品上架异常,{}", e.getMessage());
            return R.error(BizCodeException.PRODUCT_UP_EXCEPTION.getCode(), BizCodeException.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if (result) {
            return R.ok();
        } else {
            return R.error(BizCodeException.PRODUCT_UP_EXCEPTION.getCode(), BizCodeException.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }

}
